Skip to content

Go: memory leakage queries#39

Open
ex0dus-0x wants to merge 4 commits into
mainfrom
alan/go_memleak
Open

Go: memory leakage queries#39
ex0dus-0x wants to merge 4 commits into
mainfrom
alan/go_memleak

Conversation

@ex0dus-0x
Copy link
Copy Markdown
Contributor

@ex0dus-0x ex0dus-0x commented Apr 2, 2026

Add two Go queries for detecting behaviors that can lead to memory leakage:

  • DeferReleaseInLoop - deferring a resource release can cause memory leakage across iterations. This query models constrains search to common APIs where this would manifest (eg. os.OpenFile).
  • UnboundedIORead - invoking io.ReadAll of untrusted input can exhaust server memory for a denial-of-service.

@ex0dus-0x ex0dus-0x marked this pull request as ready for review April 15, 2026 19:27
@ex0dus-0x ex0dus-0x requested a review from tnytown May 14, 2026 14:33
Copy link
Copy Markdown
Member

@tnytown tnytown left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +19 to +46
ResourceAcquisition() {
this.hasQualifiedName("os", ["Open", "OpenFile", "Create", "CreateTemp", "NewFile", "Pipe"])
or
this.hasQualifiedName("net", ["Dial", "DialTimeout", "Listen", "ListenPacket"])
or
this.hasQualifiedName("net", ["FileConn", "FileListener", "FilePacketConn"])
or
this.(Method).hasQualifiedName("net", "Dialer", ["Dial", "DialContext"])
or
this.hasQualifiedName("net/http", ["Get", "Post", "PostForm", "Head"])
or
this.(Method).hasQualifiedName("net/http", "Client", ["Do", "Get", "Post", "PostForm", "Head"])
or
this.hasQualifiedName("crypto/tls", ["Dial", "DialWithDialer", "Client", "Server"])
or
this.(Method).hasQualifiedName("crypto/tls", "Dialer", "DialContext")
or
this.hasQualifiedName("compress/gzip", ["NewReader", "NewWriter", "NewWriterLevel"])
or
this.hasQualifiedName("compress/zlib",
["NewReader", "NewWriter", "NewWriterLevel", "NewWriterLevelDict"])
or
this.hasQualifiedName("compress/flate", ["NewReader", "NewWriter"])
or
this.hasQualifiedName("compress/lzw", ["NewReader", "NewWriter"])
or
this.hasQualifiedName("archive/zip", "OpenReader")
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be parameterized with MaD?

Comment on lines +80 to +81
// defer resp.Body.Close() — base is a selector, take its base identifier
result.asExpr() = base.(SelectorExpr).getBase().(Ident)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking out loud: can selectors be nested? does this work for e.g. a.b.c.Close()?

i think this doesn't matter for resources in the current list. net/http is a bit of an odd duck since Close isn't on the top-level Response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants